#include <iostream>
#include <string>
#include <sstream>
#include <iterator>
using namespace std;

#include "room.h"
#include "building.h"
#include "buildingDatabase.h"
#include "commercialBuilding.h"
#include "foodEnterpriseBuilding.h"
#include "coffeeHouse.h"
#include "hotel.h"
#include "restaurant.h"
#include "agriculturalBuilding.h"
#include "residentialBuilding.h"
#include "apartmentBuilding.h"
#include "villa.h"

void main3_1_2()		//betyg 3 uppg 1 & 2
{
	room r1;
	room r2(52);
	room r3(52, room::type_of_room::kitchen);
	r3.setArea(10);
	r3.setType(room::type_of_room::livingroom);
	r3.write();

	r2.read();
	r2.write();
}

void main3_3()		//betyg 3 uppg 3
{
	room r1(52, room::type_of_room::bedroom);
	cout << r1 << endl;

	room r2;
	string s = "type=kitchen;area=50";
	istringstream iss(s);
	iss >> r2;
	cout << r2 << endl;
	r2.write();
}

void main3_4()		//betyg 3 uppg 4
{
	building b;
	room r1;
	room r2(52, room::type_of_room::kitchen);
	room r3(10);

	b.add(r1);
	b.add(r2);
	b.add(r3);

	room r4 = *(b.search(r3));
	b.add(r4);

	cout << b.list() << endl;

	b.remove(r1);

	cout << b.list() << endl;
}

void main3_5_6()		//betyg 3 uppg 5 & 6
{
	building b;
	building b2;
	room r1;
	room r2(52, room::type_of_room::kitchen);
	room r3(10);

	b.add(r1);
	b.add(r2);
	b.add(r3);

	b.writeToFile("main3_5.txt");

	b2.readFromFile("main3_5.txt");
	cout << b2.list() << endl;
}

void main3_7()		//betyg 3 uppg 7
{
	building b;
	room r1;
	room r2(52, room::type_of_room::kitchen);
	room r3(10);

	b.add(r1);
	b.add(r2);
	b.add(r3);

	cout << "total area i b: " << b.area() << endl;
}

void main4_1(){
	buildingDatabase bdb;
	building b("byggnad1");
	building b2("byggnad2");
	room r1;
	room r2(52, room::type_of_room::kitchen);
	room r3(10);
	room r4(20);
	room r5(25, room::type_of_room::livingroom);

	b.add(r1);
	b.add(r2);
	b.add(r3);

	b2.add(r4);
	b2.add(r5);
	
	bdb.push_back(b);
	//bdb.push_back(b2);

	bdb.writeFile("main4.txt");

	buildingDatabase bdb2;
	bdb2.readFile("main4.txt");
	bdb2.push_back(b2);

	cout << "bdb.area(): " << bdb.area() << endl;
	cout << "bdb2.area(): " << bdb2.area() << endl;

	buildingDatabase::iterator it = bdb2.search("byggnad3");

	if (it != bdb2.end()){
		cout << (*it).list() << endl;
	}
	

}

void main4_2()
{
	buildingDatabase bdb;
	bdb.newBuilding();
	bdb.newBuilding();
	for (int i = 0; i < bdb.size(); i++)
	{
		cout << bdb[i].list();
	}
}

void main4_3()	//menysnurra
{
	buildingDatabase bdb;
	buildingDatabase::iterator it;
	int input;
	string temp;
	do{
		system("cls");
		cout << "1: Read File" << endl;
		cout << "2: Write File" << endl;
		cout << "3: Calculate Area" << endl;
		cout << "4: Find Building" << endl;
		cout << "5: Add Building" << endl;
		cout << "6: Delete Building" << endl;
		cout << "0: Exit" << endl << endl;

		cin >> input;
		cin.get();

		switch (input){
		case 1:
			cout << "filename: ";
			getline(cin, temp);
			bdb.readFile(temp);
			cout << "Done!" << endl;
			break;
		case 2:
			cout << "filename: ";
			getline(cin, temp);
			bdb.writeFile(temp);
			cout << "Done!" << endl;
			break;
		case 3:
			cout << "Total area = " << bdb.area() << endl;
			break;
		case 4:
			cout << "Enter info: ";
			getline(cin, temp);
			it = bdb.search(temp);
			if (it != bdb.end()){
				cout << "Found building: " << endl;
				cout << (*it).list() << endl;
			}
			break;
		case 5:
			bdb.newBuilding();
			cout << "Done!" << endl;
			break;
		case 6:
			cout << "Enter info: ";
			getline(cin, temp);
			bdb.deleteBuilding(temp);
			break;
		default:
			break;
		}

		if (input != 0){
			system("pause");
		}
		
	} while (input != 0);
}


void print(building &b)
{
	b.print();
}
void main5()
{
	hotel h("towers hotel", 40);
	restaurant rs("jacques restaurant", restaurant::kind_of_restaurant::french, 20);
	coffeeHouse ch("starbucks coffee", 50);
	foodEnterpriseBuilding feb;
	agriculturalBuilding ab("farmhouse");
	apartmentBuilding apb("seaside ave 1 apartments", 100);
	villa v("seaside ave 2 villa");

	room kitchen(20, room::type_of_room::kitchen);	
	room bed(15, room::type_of_room::bedroom);
	
	room bed2(17, room::type_of_room::bedroom);
	room bed3(5, room::type_of_room::bedroom);
	room living(30, room::type_of_room::livingroom);

	h.add(kitchen);
	rs.add(kitchen);
	apb.add(bed);
	apb.add(bed2);
	apb.add(bed3);
	v.add(bed);
	v.add(living);

	vector<building*> bv;
	bv.push_back(&h);
	bv.push_back(&rs);
	bv.push_back(&ch);
	bv.push_back(&feb);
	bv.push_back(&ab);
	bv.push_back(&apb);
	bv.push_back(&v);

	for (int i = 0; i < bv.size(); i++)
	{
		print(*(bv[i]));
		cout << endl;
	}

}

void main5_2()   //try-catch
{
	try{
		room r(20, room::type_of_room::kitchen);
		r.write();
	}
	catch(buildingException& be){
		be.print();
		cout << be.what();
	}

	cout << endl;

	try{
		room r2(-5, room::type_of_room::bedroom);
		r2.write();
	}
	catch (buildingException& be){
		be.print();
		cout << be.what();
	}

}

int main(){
	//main3_1_2();
	//main3_3();
	//main3_4();
	//main3_5_6();
	//main3_7();
	//main4_1();
	//main4_2();
	//main4_3();
	//main5();
	main5_2();
	system("pause");
	return 0;
}